package com.sairobo.app.modules.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sairobo.base.common.exception.RRException;
import com.sairobo.base.common.validator.Assert;
import com.sairobo.app.modules.app.dao.UserDao;
import com.sairobo.app.modules.app.entity.UserEntity;
import com.sairobo.app.modules.app.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao,UserEntity> implements UserService {

    @Autowired
	private UserDao userDao;
	
	@Override
	public UserEntity queryObject(Long userId){
		return baseMapper.selectById(userId);
	}

	@Override
	public void save(String mobile, String password){
		UserEntity user = new UserEntity();
		user.setMobile(mobile);
		user.setUsername(mobile);
        System.out.println(DigestUtils.sha256Hex(password));
        user.setPassword(DigestUtils.sha256Hex(password));
		user.setCreateTime(new Date());
        System.out.println(user.toString());
        userDao.insert(user);
	}

	@Override
	public UserEntity queryByMobile(String mobile) {
		return userDao.queryByMobile(mobile);
	}

	@Override
	public long login(String mobile, String password) {
		UserEntity user = queryByMobile(mobile);
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(password))){
			throw new RRException("手机号或密码错误");
		}

		return user.getUserId();
	}
}
